<?php
namespace api\helpers;

use Yii;
use yii\base\Component;
use yii\base\InvalidCallException;
use yii\httpclient\Client;
use common\models\user\User;

/**
 * Logistics 物流API
 */
class Logistics extends Component
{

    public static function getLogistics($nu, $com = 'shunfeng')
    {
        $host = "https://ali-deliver.showapi.com";
        $path = "/showapi_expInfo";
        $method = "GET";
        $appcode = "5617b34ae2ee4412926816dd9ded5631";
        $headers = [];
        array_push($headers, "Authorization:APPCODE " . $appcode);
        $querys = "com=" . $com . "&nu=" . $nu;
        $bodys = "";
        $url = $host . $path . "?" . $querys;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $res = curl_exec($curl);
        curl_close($curl);
        return json_decode($res);
    }

    public static function getLog($nu)
    {
        if(!isset($nu)) return '暂无物流信息';
        if($nu == '0') return '同城配送中';
        $res = self::getLogistics($nu);
        if($res->showapi_res_code !== 0){
            echo '查不到货运信息,请与商家联系:单号为'. $nu;
        }else{
            return $res->showapi_res_body->data;
        }
    }


}
